package com.newtouch.bxzs.business.personal.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Objects;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.newtouch.bxzs.business.enclosure.service.AttachmentService;
import com.newtouch.bxzs.business.personal.mapper.FeedbackProblemMapper;
import com.newtouch.bxzs.business.personal.mapper.PersonalMapper;
import com.newtouch.bxzs.business.personal.model.AddFeedbackProblemVO;
import com.newtouch.bxzs.business.personal.model.AnswerFeedbackProblemVO;
import com.newtouch.bxzs.business.personal.model.FeedbackProblemBO;
import com.newtouch.bxzs.business.personal.model.FeedbackProblemVO;
import com.newtouch.bxzs.business.personal.service.FeedbackProblemService;
import com.newtouch.bxzs.common.base.BaseErrorEnum;
import com.newtouch.bxzs.common.base.BaseException;
import com.newtouch.bxzs.common.base.em.FeedbackProblemStatusEnum;
import com.newtouch.bxzs.common.base.em.UserFlagEnum;
import com.newtouch.bxzs.common.base.model.FeedbackProblem;
import com.newtouch.bxzs.common.base.model.User;
import com.newtouch.bxzs.common.base.model.vo.EnclosureVO;
import com.newtouch.bxzs.common.base.util.UserDetailUtil;

import cn.hutool.core.bean.BeanUtil;

/**
 * 反馈问题服务实现类
 *
 * <p>
 * <b>History:</b>
 * <table border="1">
 * <tr>
 * <th>Date</th>
 * <th>Operator</th>
 * <th>Memo</th>
 * </tr>
 * <tr>
 * <td>2020年12月30日</td>
 * <td>xiongdianqiang</td>
 * <td>Create</td>
 * </tr>
 * </table>
 * 
 * @author xiongdianqiang
 * 
 * @version 1.0.0
 * @since 1.0.0
 */
@Service
public class FeedbackProblemServiceImpl implements FeedbackProblemService {
	@Autowired
	private FeedbackProblemMapper feedbackProblemMapper;

	@Autowired
	private PersonalMapper personalMapper;

	@Autowired
	private AttachmentService attachmentService;

	/** 静态变量：系统日志 */
	private static final Log logger = LogFactory.getLog(FeedbackProblemServiceImpl.class);

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.newtouch.bxzs.business.personal.service.FeedbackProblemService#
	 * getFeedbackProblemList(com.newtouch.bxzs.business.personal.model.
	 * FeedbackProblemVO)
	 */
	@Override
	public IPage<FeedbackProblemBO> getFeedbackProblemList(FeedbackProblemVO feedbackProblemVO) {
		if (feedbackProblemVO == null) {
			throw new BaseException(BaseErrorEnum.REQUEST_DATA_NULL);
		}
		if (feedbackProblemVO.getUserFlag() == null) {
			throw new BaseException("客户端请求标志不能为空！");
		}
		logger.info("分页查询反馈问题");
		Page<FeedbackProblem> page = new Page<>();
		page.setCurrent(feedbackProblemVO.getCurrentPage());
		page.setSize(feedbackProblemVO.getPageSize());
		String userOrganizationCode = Objects.requireNonNull(UserDetailUtil.getUserDetail().getUserOrganizationCode());
		String userId = Objects.requireNonNull(UserDetailUtil.getUserDetail().getUserId());
		// pc端获取本级机构人员反馈的问题列表
		if (UserFlagEnum.PC_FLAG.getCode().equals(feedbackProblemVO.getUserFlag())) {
			return feedbackProblemMapper.getPcFeedbackProblemList(page, feedbackProblemVO, userOrganizationCode);
		} else {
			// app端根据用户工号获取反馈信息列表信息
			return feedbackProblemMapper.getAppFeedbackProblemList(page, feedbackProblemVO, userId);
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.newtouch.bxzs.business.personal.service.FeedbackProblemService#
	 * getFeedbackProblemDetailById(java.lang.String)
	 */
	@Override
	public FeedbackProblemBO getFeedbackProblemDetailById(String problemFeedbackId) {
		if (StringUtils.isEmpty(problemFeedbackId)) {
			throw new BaseException(BaseErrorEnum.REQUEST_DATA_NULL);
		}
		/* 获取附件 */
		HashMap<String, List<String>> result = attachmentService
				.getEnclosureList(new EnclosureVO(problemFeedbackId, null));
		FeedbackProblem feedbackProblem = feedbackProblemMapper.selectById(problemFeedbackId);
		if (BeanUtil.isEmpty(feedbackProblem)) {
			throw new BaseException("该问题不存在");
		}
		String userId = feedbackProblem.getCreateUserId();
		User user = personalMapper.selectById(userId);
		FeedbackProblemBO feedbackProblemBO = new FeedbackProblemBO();
		BeanUtil.copyProperties(feedbackProblem, feedbackProblemBO);
		if (!BeanUtil.isEmpty(user)) {
			feedbackProblemBO.setUserName(user.getUserName());
			feedbackProblemBO.setUserJobNumber(user.getUserJobNumber());
		}
		if (result != null) {
			// 设置缩略图地址
			feedbackProblemBO.setAttachmentUrls(result);
		}
		return feedbackProblemBO;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.newtouch.bxzs.business.personal.service.FeedbackProblemService#
	 * answerFeedbackProblem(com.newtouch.bxzs.business.personal.model.
	 * AnswerFeedbackProblemVO)
	 */
	@Override
	public FeedbackProblemBO answerFeedbackProblem(AnswerFeedbackProblemVO answerFeedbackProblemVO) {
		String problemFeedbackId = answerFeedbackProblemVO.getProblemFeedbackId();
		if (StringUtils.isEmpty(problemFeedbackId)) {
			throw new BaseException(BaseErrorEnum.REQUEST_DATA_NULL);
		}
		if (StringUtils.isEmpty(answerFeedbackProblemVO.getFeedbackProblemAnswerDescription())
				|| StringUtils.isEmpty(answerFeedbackProblemVO.getFeedbackProblemStatus())) {
			throw new BaseException("必录项未填写！");
		}
		if (!StringUtils.isEmpty(answerFeedbackProblemVO.getFeedbackProblemAnswerDescription())
				&& answerFeedbackProblemVO.getFeedbackProblemAnswerDescription().length() > 300) {
			throw new BaseException("答案长度不能超过300！");
		}
		FeedbackProblem feedbackProblem = feedbackProblemMapper.selectById(problemFeedbackId);
		String userId = feedbackProblem.getCreateUserId();
		User user = personalMapper.selectById(userId);
		if (!BeanUtil.isEmpty(feedbackProblem)) {
			feedbackProblem
					.setFeedbackProblemAnswerDescription(answerFeedbackProblemVO.getFeedbackProblemAnswerDescription());
			String problemFeedbackVOStatus = answerFeedbackProblemVO.getFeedbackProblemStatus();
			String feedbackProblemEnumStatus = FeedbackProblemStatusEnum.ANSWERED_STATUS.getCode();
			if (feedbackProblemEnumStatus.equals(problemFeedbackVOStatus)) {
				feedbackProblem.setFeedbackProblemStatus(answerFeedbackProblemVO.getFeedbackProblemStatus());
				feedbackProblemMapper.updateById(feedbackProblem);
			}
		}
		FeedbackProblemBO feedbackProblemBO = new FeedbackProblemBO();
		BeanUtil.copyProperties(feedbackProblem, feedbackProblemBO);
		feedbackProblemBO.setUserName(user.getUserName());
		feedbackProblemBO.setUserJobNumber(user.getUserJobNumber());
		/* 获取附件 */
		HashMap<String, List<String>> result = attachmentService
				.getEnclosureList(new EnclosureVO(problemFeedbackId, null));
		if (result != null) {
			// 设置缩略图地址
			feedbackProblemBO.setAttachmentUrls(result);
		}
		return feedbackProblemBO;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.newtouch.bxzs.business.personal.service.FeedbackProblemService#
	 * addFeedbackProblem(com.newtouch.bxzs.business.personal.model.
	 * AddFeedbackProblemVO, org.springframework.web.multipart.MultipartFile)
	 */
	@Override
	public String addFeedbackProblem(AddFeedbackProblemVO addFeedbackProblemVO) {
		if (addFeedbackProblemVO == null) {
			throw new BaseException(BaseErrorEnum.REQUEST_DATA_NULL);
		}
		String userId = Objects.requireNonNull(UserDetailUtil.getUserDetail()).getUserId();
		User user = personalMapper.selectById(userId);
		if (BeanUtil.isEmpty(user)) {
			throw new BaseException(BaseErrorEnum.USER_NOT_EXISTS);
		}
		String problemFeedbackTitle = addFeedbackProblemVO.getFeedbackProblemTitle();
		String problemFeedbackContent = addFeedbackProblemVO.getFeedbackProblemContent();
		if (StringUtils.isEmpty(problemFeedbackTitle) || StringUtils.isEmpty(problemFeedbackContent)) {
			throw new BaseException("必录项未填写！");
		}
		if (!StringUtils.isEmpty(problemFeedbackTitle) && problemFeedbackTitle.length() > 30) {
			throw new BaseException("标题不能超过30个字符！");
		}
		if (!StringUtils.isEmpty(problemFeedbackContent) && problemFeedbackContent.length() > 300) {
			throw new BaseException("内容不能超过300个字符！");
		}
		FeedbackProblem feedbackProblem = new FeedbackProblem();
		feedbackProblem.setCreateUserId(userId);
		feedbackProblem.setCreateTime(new Date());
		feedbackProblem.setFeedbackProblemStatus(FeedbackProblemStatusEnum.UNANSWERED_STATUS.getCode());
		feedbackProblem.setUpdateUserId(userId);
		feedbackProblem.setUpdateTime(new Date());
		feedbackProblem.setFeedbackProblemTitle(problemFeedbackTitle);
		feedbackProblem.setFeedbackProblemContent(problemFeedbackContent);
		feedbackProblemMapper.insert(feedbackProblem);
		return feedbackProblem.getFeedbackProblemId();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.newtouch.bxzs.business.personal.service.FeedbackProblemService#
	 * revokeFeedbackProblem(java.lang.String)
	 */
	@Override
	public void revokeFeedbackProblem(String problemFeedbackId) {
		if (problemFeedbackId == null) {
			throw new BaseException(BaseErrorEnum.REQUEST_DATA_NULL);
		}
		FeedbackProblem feedbackProblem = feedbackProblemMapper.selectById(problemFeedbackId);
		if (BeanUtil.isEmpty(feedbackProblem)) {
			throw new BaseException("反馈问题不存在 ，撤回失败！");
		}
		String feedbackProblemStatus = FeedbackProblemStatusEnum.ANSWERED_STATUS.getCode();
		if (feedbackProblem.getFeedbackProblemStatus().equals(feedbackProblemStatus)) {
			throw new BaseException("该问题已解答，不可撤回!");
		}
		// 撤回反馈问题，更改状态为已撤回
		feedbackProblem.setFeedbackProblemStatus(FeedbackProblemStatusEnum.REVOKE_STATUS.getCode());
		feedbackProblemMapper.updateById(feedbackProblem);
	}
}
